import { Component, OnInit } from '@angular/core';
import { NzMessageService, NzModalService, UploadFile } from 'ng-zorro-antd';
import { EmitterService } from '../../../service/emit.service';
import { ProductService } from '../../../service/prod/product.service';
import {of} from "rxjs/index";
import {catchError} from "rxjs/operators";
import {sysqmService} from "../../../service/sysqm/sysqm.service";

@Component({
    selector: 'product-sku-edit-v2',
    styleUrls: ['./product-sku-edit-v2.component.less'],
    templateUrl: './product-sku-edit-v2.component.html'
})



export class ProductSkuEditV2Component implements OnInit {
  loading = false;

  qmpage:  any = {
    qmcode1:"",
    qmcode2:"",
    qmcode3:"",
    qmcode4:"",
    qmcode5:"",
    qmcode6:"",
    qmcode7:"",
    qmcode8:"",
    qmcode9:"",
    qmcode10:"",
    qmcode11:"",
    qmcode12:"",
    qmcode13:"",
    qmcode14:"",
    qmcode15:"",
    qmcode16:"",
    qmcode17:"",
    qmcode18:"",
    qmcode19:"",
    qmcode20:"",

    qmid1:"",
    qmid2:"",
    qmid3:"",
    qmid4:"",
    qmid5:"",
    qmid6:"",
    qmid7:"",
    qmid8:"",
    qmid9:"",
    qmid10:"",
    qmid11:"",
    qmid12:"",
    qmid13:"",
    qmid14:"",
    qmid15:"",
    qmid16:"",
    qmid17:"",
    qmid18:"",
    qmid19:"",
    qmid20:"",


    qmname1:"",
    qmvalue1: "",
    qmname2:"",
    qmvalue2: "",
    qmname3:"",
    qmvalue3: "",
    qmname4:"",
    qmvalue4: "",
    qmname5:"",
    qmvalue5: "",
    qmname6:"",
    qmvalue6: "",
    qmname7:"",
    qmvalue7: "",
    qmname8:"",
    qmvalue8: "",
    qmname9:"",
    qmvalue9: "",
    qmname10:"",
    qmvalue10: "",
    qmname11:"",
    qmvalue11: "",
    qmname12:"",
    qmvalue12: "",
    qmname13:"",
    qmvalue13: "",
    qmname14:"",
    qmvalue14: "",
    qmname15:"",
    qmvalue15: "",
    qmname16:"",
    qmvalue16: "",
    qmname17:"",
    qmvalue17: "",
    qmname18:"",
    qmvalue18: "",
    qmname19:"",
    qmvalue19: "",
    qmname20:"",
    qmvalue20: ""
  };
  qmvis:  any = {
    qm1:0,
    qm2:0,
    qm3:0,
    qm4:0,
    qm5:0,
    qm6:0,
    qm7:0,
    qm8:0,
    qm9:0,
    qm10:0,
    qm11:0,
    qm12:0,
    qm13:0,
    qm14:0,
    qm15:0,
    qm16:0,
    qm17:0,
    qm18:0,
    qm19:0,
    qm20:0

  };

  message = "扫描送货单条码完成收货，质检，并同步到SAP，存在物料凭证才可做105和质检";
  messagesum="以下为按物料号合计数量";
  messageqm="检验结果";
  messageok = "  ";

    // attrListc = [ '103凭证',   '箱号',
    //   '采购订单号', '行项目', '雅瑞物料号',
    //   '供应商物料','库存地点', '送货数量', '实收数', '良品数', '不良品数',  '备注',"抽检数量","不良比例","主要不良","质检备注","判定","104凭证","质检员","105凭证"];
    dataTable = [];
    dataTablebak = [];
  qmbutton="保存";
  qminputs=0;
  dataTablesum = [];
  description
  uploadUrl = '';
    batch = [];
  pageIndex = 1;
  pageSize = 10;
  total = 1;
  lps=0;
  blps=0;
   id="";
   qmname="";
   qmtype="";
    constructor(
      private msg: NzMessageService,
        private modalService: NzModalService,
        private emit: EmitterService,
      private sysqmService: sysqmService,

      private productService: ProductService
    ) {

    }
    ngOnInit() {
      this.qminputs=0;
        this.batch = [];
    }
  searchData(): void {
    this.dataTable = this.dataTablebak.slice((this.pageIndex - 1) * this.pageSize, (this.pageIndex) * this.pageSize);
  }
    append(rwocount) {
        if (this.dataTable.length >= 10) {
            return;
        }
        for(let row = 0;row<=rwocount;row++){
          this.dataTable.push([]);
        }
    }
    batchEdit(index) {
      this.lps  = 0;
      this.blps = 0;
      this.dataTable[index][9] =   Number(this.dataTable[index][7])-Number(this.dataTable[index][10]);
      this.dataTable[index][21] =   Number(this.dataTable[index][11])-Number(this.dataTable[index][20]);
      for (let row = 0; row < this.dataTable.length; row++) {
              this.lps = Number(this.lps)+Number(this.dataTable[row][9]);
              this.blps = Number(this.blps)+Number(this.dataTable[row][10]);
            }
           this.message = "扫描送货单条码完成收货，质检，并同步到SAP，存在物料凭证才可做105和质检,检验结果：良品数："+this.lps+"    不良品数："+this.blps;
    }

    iniqm(){
      this.qmvis.qm1 = 0;
      this.qmvis.qm2= 0;
      this.qmvis.qm3= 0;
      this.qmvis.qm4= 0;
      this.qmvis.qm5= 0;
      this.qmvis.qm6= 0;
      this.qmvis.qm7= 0;
      this.qmvis.qm8= 0;
      this.qmvis.qm9= 0;
      this.qmvis.qm10= 0;
      this.qmvis.qm11= 0;
      this.qmvis.qm12= 0;
      this.qmvis.qm13= 0;
      this.qmvis.qm14= 0;
      this.qmvis.qm15= 0;
      this.qmvis.qm16= 0;
      this.qmvis.qm17= 0;
      this.qmvis.qm18= 0;
      this.qmvis.qm19= 0;
      this.qmvis.qm20= 0;


      this.qmpage.qmcode1="";
      this.qmpage.qmcode2="";
      this.qmpage.qmcode3="";
      this.qmpage.qmcode4="";
      this.qmpage.qmcode5="";
      this.qmpage.qmcode6="";
      this.qmpage.qmcode7="";
      this.qmpage.qmcode8="";
      this.qmpage.qmcode9="";
      this.qmpage.qmcode10="";
      this.qmpage.qmcode11="";
      this.qmpage.qmcode12="";
      this.qmpage.qmcode13="";
      this.qmpage.qmcode14="";
      this.qmpage.qmcode15="";
      this.qmpage.qmcode16="";
      this.qmpage.qmcode17="";
      this.qmpage.qmcode18="";
      this.qmpage.qmcode19="";
      this.qmpage.qmcode20="";

      this.qmpage.qmid1="";
      this.qmpage.qmid2="";
      this.qmpage.qmid3="";
      this.qmpage.qmid4="";
      this.qmpage.qmid5="";
      this.qmpage.qmid6="";
      this.qmpage.qmid7="";
      this.qmpage.qmid8="";
      this.qmpage.qmid9="";
      this.qmpage.qmid10="";
      this.qmpage.qmid11="";
      this.qmpage.qmid12="";
      this.qmpage.qmid13="";
      this.qmpage.qmid14="";
      this.qmpage.qmid15="";
      this.qmpage.qmid16="";
      this.qmpage.qmid17="";
      this.qmpage.qmid18="";
      this.qmpage.qmid19="";
      this.qmpage.qmid20="";

    }
 qminput(index,qmtype){
this.iniqm();

   this.loading=true;
   this.qminputs=1;
   this.qmtype=qmtype;
   if(this.qmtype=="1"){
     this.qmbutton = "外观保存";
   }
   if(this.qmtype=="2"){
     this.qmbutton = "测试保存";
   }

   this.qmname=this.dataTable[index][4]+":"+this.dataTable[index][7];
   this.messageqm="正在检验;"+this.qmname;

   this.id=this.dataTable[index][22] ;

   this.sysqmService.getqmlist(this.id,qmtype).pipe(
     catchError(err => {
       return of(err);
     })
   ).subscribe(data => {
     this.loading=false;

     console.log("data******="+JSON.stringify(data));
     if(data.ok){

       if (data.data.length > 0) {
         let i = 0;
         for (const detail of data.data) {
            i = i+1;
           //显示内容打开
           if(i==1){
             this.qmvis.qm1=1
             this.qmpage.qmid1=detail.id;

             this.qmpage.qmcode1=detail.qmCode;
             this.qmpage.qmname1=detail.qmName;
             this.qmpage.qmvalue1=detail.qmBlpquantity;
           }
           if(i==2){
             this.qmvis.qm2=1
             this.qmpage.qmid2=detail.id;

             this.qmpage.qmcode2=detail.qmCode;
             this.qmpage.qmname2= detail.qmName;
             this.qmpage.qmvalue2=detail.qmBlpquantity;
           }
           if(i==3){
             this.qmvis.qm3=1
             this.qmpage.qmid3=detail.id;
             this.qmpage.qmcode3=detail.qmCode;
             this.qmpage.qmname3= detail.qmName;
             this.qmpage.qmvalue3=detail.qmBlpquantity;
           }
           if(i==4){
             this.qmvis.qm4=1
             this.qmpage.qmid4=detail.id;

             this.qmpage.qmcode4=detail.qmCode;
             this.qmpage.qmname4= detail.qmName;
             this.qmpage.qmvalue4=detail.qmBlpquantity;
           }
           if(i==5){
             this.qmvis.qm5=1
             this.qmpage.qmid5=detail.id;

             this.qmpage.qmcode5=detail.qmCode;
             this.qmpage.qmname5= detail.qmName;
             this.qmpage.qmvalue5=detail.qmBlpquantity;
           }
           if(i==6){
             this.qmvis.qm6=1
             this.qmpage.qmid6=detail.id;

             this.qmpage.qmcode6=detail.qmCode;
             this.qmpage.qmname6=detail.qmName;
             this.qmpage.qmvalue6=detail.qmBlpquantity;
           }
           if(i==7){
             this.qmvis.qm7=1
             this.qmpage.qmid7=detail.id;

             this.qmpage.qmcode7=detail.qmCode;
             this.qmpage.qmname7= detail.qmName;
             this.qmpage.qmvalue7=detail.qmBlpquantity;
           }
           if(i==8){
             this.qmvis.qm8=1
             this.qmpage.qmid8=detail.id;
             this.qmpage.qmcode8=detail.qmCode;
             this.qmpage.qmname8= detail.qmName;
             this.qmpage.qmvalue8=detail.qmBlpquantity;
           }
           if(i==9){
             this.qmvis.qm9=1
             this.qmpage.qmid9=detail.id;

             this.qmpage.qmcode9=detail.qmCode;
             this.qmpage.qmname9= detail.qmName;
             this.qmpage.qmvalue9=detail.qmBlpquantity;
           }
           if(i==10){
             this.qmvis.qm10=1
             this.qmpage.qmid10=detail.id;

             this.qmpage.qmcode10=detail.qmCode;
             this.qmpage.qmname10= detail.qmName;
             this.qmpage.qmvalue10=detail.qmBlpquantity;
           }

           //显示内容赋值

           if(i==11){
             this.qmvis.qm11=1
             this.qmpage.qmid11=detail.id;

             this.qmpage.qmcode11=detail.qmCode;
             this.qmpage.qmname11=detail.qmName;
             this.qmpage.qmvalue11=detail.qmBlpquantity;
           }
           if(i==12){
             this.qmvis.qm12=1
             this.qmpage.qmid12=detail.id;

             this.qmpage.qmcode12=detail.qmCode;
             this.qmpage.qmname12= detail.qmName;
             this.qmpage.qmvalue12=detail.qmBlpquantity;
           }
           if(i==13){
             this.qmvis.qm13=1
             this.qmpage.qmid13=detail.id;
             this.qmpage.qmcode13=detail.qmCode;
             this.qmpage.qmname13= detail.qmName;
             this.qmpage.qmvalue13=detail.qmBlpquantity;
           }
           if(i==14){
             this.qmvis.qm14=1
             this.qmpage.qmid14=detail.id;

             this.qmpage.qmcode14=detail.qmCode;
             this.qmpage.qmname14= detail.qmName;
             this.qmpage.qmvalue14=detail.qmBlpquantity;
           }
           if(i==15){
             this.qmvis.qm15=1
             this.qmpage.qmid15=detail.id;

             this.qmpage.qmcode15=detail.qmCode;
             this.qmpage.qmname15= detail.qmName;
             this.qmpage.qmvalue15=detail.qmBlpquantity;
           }
           if(i==16){
             this.qmvis.qm16=1
             this.qmpage.qmid16=detail.id;

             this.qmpage.qmcode16=detail.qmCode;
             this.qmpage.qmname16=detail.qmName;
             this.qmpage.qmvalue16=detail.qmBlpquantity;
           }
           if(i==17){
             this.qmvis.qm17=1
             this.qmpage.qmid17=detail.id;

             this.qmpage.qmcode17=detail.qmCode;
             this.qmpage.qmname17= detail.qmName;
             this.qmpage.qmvalue17=detail.qmBlpquantity;
           }
           if(i==18){
             this.qmvis.qm18=1
             this.qmpage.qmid18=detail.id;
             this.qmpage.qmcode18=detail.qmCode;
             this.qmpage.qmname18= detail.qmName;
             this.qmpage.qmvalue18=detail.qmBlpquantity;
           }
           if(i==19){
             this.qmvis.qm19=1
             this.qmpage.qmid19=detail.id;

             this.qmpage.qmcode19=detail.qmCode;
             this.qmpage.qmname19= detail.qmName;
             this.qmpage.qmvalue19=detail.qmBlpquantity;
           }
           if(i==20){
             this.qmvis.qm20=1
             this.qmpage.qmid20=detail.id;

             this.qmpage.qmcode20=detail.qmCode;
             this.qmpage.qmname20= detail.qmName;
             this.qmpage.qmvalue20=detail.qmBlpquantity;
           }




         }
       }
       }else{
       this.msg.error( data.msg);

     }

   })
 }

  saveqm() {

    this.loading=true;

    let syspjPagelist = [];
    if (this.qmpage.qmcode1 != ""){
      let dto = {
        id:this.qmpage.qmid1,

        qmCode: this.qmpage.qmcode1,
        qmName: this.qmpage.qmname1,
        qmBlpquantity: this.qmpage.qmvalue1

      }
    syspjPagelist.push(dto);
  }
    if (this.qmpage.qmcode2 != ""){
      let dto = {
        id:this.qmpage.qmid2,
        qmCode: this.qmpage.qmcode2,
        qmName: this.qmpage.qmname2,
        qmBlpquantity: this.qmpage.qmvalue2

      }
      syspjPagelist.push(dto);
    }

    if (this.qmpage.qmcode3 != ""){
      let dto = {
        id:this.qmpage.qmid3,

        qmCode: this.qmpage.qmcode3,
        qmName: this.qmpage.qmname3,
        qmBlpquantity: this.qmpage.qmvalue3

      }
      syspjPagelist.push(dto);
    }
    if (this.qmpage.qmcode4 != ""){
      let dto = {
        id:this.qmpage.qmid4,

        qmCode: this.qmpage.qmcode4,
        qmName: this.qmpage.qmname4,
        qmBlpquantity: this.qmpage.qmvalue4

      }
      syspjPagelist.push(dto);
    }

    if (this.qmpage.qmcode5 != ""){
      let dto = {
        id:this.qmpage.qmid5,

        qmCode: this.qmpage.qmcode5,
        qmName: this.qmpage.qmname5,
        qmBlpquantity: this.qmpage.qmvalue5

      }
      syspjPagelist.push(dto);
    }

    if (this.qmpage.qmcode6 != ""){
      let dto = {
        id:this.qmpage.qmid6,

        qmCode: this.qmpage.qmcode6,
        qmName: this.qmpage.qmname6,
        qmBlpquantity: this.qmpage.qmvalue6

      }
      syspjPagelist.push(dto);
    }
    if (this.qmpage.qmcode7 != ""){
      let dto = {
        id:this.qmpage.qmid7,
        qmCode: this.qmpage.qmcode7,
        qmName: this.qmpage.qmname7,
        qmBlpquantity: this.qmpage.qmvalue7

      }
      syspjPagelist.push(dto);
    }

    if (this.qmpage.qmcode8 != ""){
      let dto = {
        id:this.qmpage.qmid8,

        qmCode: this.qmpage.qmcode8,
        qmName: this.qmpage.qmname8,
        qmBlpquantity: this.qmpage.qmvalue8

      }
      syspjPagelist.push(dto);
    }
    if (this.qmpage.qmcode9 != ""){
      let dto = {
        id:this.qmpage.qmid9,

        qmCode: this.qmpage.qmcode9,
        qmName: this.qmpage.qmname9,
        qmBlpquantity: this.qmpage.qmvalue9

      }
      syspjPagelist.push(dto);
    }

    if (this.qmpage.qmcode10 != ""){
      let dto = {
        id:this.qmpage.qmid10,

        qmCode: this.qmpage.qmcode10,
        qmName: this.qmpage.qmname10,
        qmBlpquantity: this.qmpage.qmvalue10

      }
      syspjPagelist.push(dto);
    }

    if (this.qmpage.qmcode11 != ""){
      let dto = {
        id:this.qmpage.qmid11,

        qmCode: this.qmpage.qmcode11,
        qmName: this.qmpage.qmname11,
        qmBlpquantity: this.qmpage.qmvalue11

      }
      syspjPagelist.push(dto);
    }
    if (this.qmpage.qmcode12 != ""){
      let dto = {
        id:this.qmpage.qmid12,
        qmCode: this.qmpage.qmcode12,
        qmName: this.qmpage.qmname12,
        qmBlpquantity: this.qmpage.qmvalue12

      }
      syspjPagelist.push(dto);
    }

    if (this.qmpage.qmcode13 != ""){
      let dto = {
        id:this.qmpage.qmid13,

        qmCode: this.qmpage.qmcode13,
        qmName: this.qmpage.qmname13,
        qmBlpquantity: this.qmpage.qmvalue13

      }
      syspjPagelist.push(dto);
    }
    if (this.qmpage.qmcode14 != ""){
      let dto = {
        id:this.qmpage.qmid14,

        qmCode: this.qmpage.qmcode14,
        qmName: this.qmpage.qmname14,
        qmBlpquantity: this.qmpage.qmvalue14

      }
      syspjPagelist.push(dto);
    }

    if (this.qmpage.qmcode15 != ""){
      let dto = {
        id:this.qmpage.qmid15,

        qmCode: this.qmpage.qmcode15,
        qmName: this.qmpage.qmname15,
        qmBlpquantity: this.qmpage.qmvalue15

      }
      syspjPagelist.push(dto);
    }

    if (this.qmpage.qmcode16 != ""){
      let dto = {
        id:this.qmpage.qmid16,

        qmCode: this.qmpage.qmcode16,
        qmName: this.qmpage.qmname16,
        qmBlpquantity: this.qmpage.qmvalue16

      }
      syspjPagelist.push(dto);
    }
    if (this.qmpage.qmcode17 != ""){
      let dto = {
        id:this.qmpage.qmid17,
        qmCode: this.qmpage.qmcode17,
        qmName: this.qmpage.qmname17,
        qmBlpquantity: this.qmpage.qmvalue17

      }
      syspjPagelist.push(dto);
    }

    if (this.qmpage.qmcode18 != ""){
      let dto = {
        id:this.qmpage.qmid18,

        qmCode: this.qmpage.qmcode18,
        qmName: this.qmpage.qmname18,
        qmBlpquantity: this.qmpage.qmvalue18

      }
      syspjPagelist.push(dto);
    }
    if (this.qmpage.qmcode19 != ""){
      let dto = {
        id:this.qmpage.qmid19,

        qmCode: this.qmpage.qmcode19,
        qmName: this.qmpage.qmname19,
        qmBlpquantity: this.qmpage.qmvalue19

      }
      syspjPagelist.push(dto);
    }

    if (this.qmpage.qmcode20 != ""){
      let dto = {
        id:this.qmpage.qmid20,

        qmCode: this.qmpage.qmcode20,
        qmName: this.qmpage.qmname20,
        qmBlpquantity: this.qmpage.qmvalue20

      }
      syspjPagelist.push(dto);
    }


    let syspjPage = {
      qmBuseList:JSON.parse(JSON.stringify(syspjPagelist))
    };
    // 调用接口
     this.sysqmService.saveqmlist(syspjPage,this.id,this.qmtype).pipe(
      catchError(err => {
        return of(err);
      })
    ).subscribe(data => {
       this.loading=false;
       this.qminputs=0;

        this.msg.success("保存成功");




    })

  }

  pasteExcel(row, col, e) {
    let CHAR = '\r\n';
    if ("Windows" !== this.getOS()) {
      CHAR = '\n';
    }
    for (let item of e.clipboardData.items) {
      if (item.type === 'text/plain') {
        item.getAsString((text) => {
          let x = col,
            y = row,
            obj = {};
          text = text.trim(CHAR);
          text.split(CHAR).forEach((v2, i2) => {
            v2.split('\t').forEach((v3, i3) => {
              let newRow = y + i2, newCol = x + i3;
              if (this.dataTable[newRow]) {
                this.dataTable[newRow][newCol] = v3;
              }
            })
          })
        })
      }
    }
  }
    getOS() {
        var userAgent = window.navigator.userAgent,
            platform = window.navigator.platform,
            macosPlatforms = ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K'],
            windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'],
            iosPlatforms = ['iPhone', 'iPad', 'iPod'],
            os = null;

        if (macosPlatforms.indexOf(platform) !== -1) {
          os = 'Mac OS';
        } else if (iosPlatforms.indexOf(platform) !== -1) {
          os = 'iOS';
        } else if (windowsPlatforms.indexOf(platform) !== -1) {
          os = 'Windows';
        } else if (/Android/.test(userAgent)) {
          os = 'Android';
        } else if (!os && /Linux/.test(platform)) {
          os = 'Linux';
        }

        return os;
    }}
